package com.mechmind.robotsocket.resource;

import cn.elibot.robot.commons.lang.resource.LocaleProvider;
import cn.elibot.robot.commons.lang.resource.UTF8Control;

import java.util.Locale;
import java.util.ResourceBundle;

/**
 * Resource Support for i18n
 *
 */
public class ResourceSupport {
    private static LocaleProvider provider;
    private static final String PROPERTIES = "i18n.text";

    private ResourceBundle defaultResourceBundle;
    public static void setLocaleProvider(LocaleProvider provider) {
        ResourceSupport.provider = provider;
    }

    public static LocaleProvider getLocaleProvider() {
        return ResourceSupport.provider;
    }

    /**
     * Defaults to the language configured in the software, if that language does not exist,
     * the current system language is used, or if the system language does not exist either, "!" +key
     * @param key String
     * @return
     */
    public String getString(String key) {
        try {
            if (this.defaultResourceBundle == null) {
                defaultResourceBundle = ResourceBundle.getBundle(PROPERTIES, provider.getLocale(), new UTF8Control());
            }
            return defaultResourceBundle.getString(key);
        } catch (Exception var3) {
            return "!" + key;
        }
    }

    public static String getStringByKey(Locale locale, String key) {
        try {
            ResourceBundle resourceBundle = ResourceBundle.getBundle(PROPERTIES, locale, new UTF8Control());
            return resourceBundle.getString(key);
        } catch (Exception var3) {
            return "!" + key;
        }
    }
}
